"""
文本餐宿解析，参数按行存储，例：
同类参数可重复, #为注释
sourcepath=E:/data
sourcepath=E:/data

logpath=E:/wirged
"""

def parseparams(lines = None):
    """
    lines param 【“param”,"param2"】参数列表,
    """

    paramsdict = {}

    if lines == None:
        print("params lines is null")
        return None

    for line in lines:
        text = line.strip()
        if text == "" or len(text) == 0:
            continue
        if text[0] == "#":
            continue

        paramlist = text.split("=")
        if paramlist == None or len(paramlist) <= 1:
            continue

        if len(paramlist) == 2:
            key = paramlist[0]
            value = paramlist[1]

            key = key.strip()
            if len(key) == 0:
                continue
            value = value.strip()

            if len(value) == 0:
                continue

            valuelist = None
            """字典中已经存在的值"""
            existvalue = None

            try:
                existvalue = paramsdict[key]
            except Exception as e:
                existvalue = None

            if existvalue == None:
                valuelist = None
            else:
                if isinstance(existvalue, list):
                    valuelist = existvalue
                    valuelist.append(value)
                if isinstance(existvalue, str):
                    valuelist = []
                    valuelist.append(existvalue)
                    valuelist.append(value)
            if existvalue == None:
                paramsdict[key] = value
            if existvalue and valuelist:
                paramsdict[key] = valuelist

    return paramsdict

def getparamsfromparamdict(d, paramkey, paramtype="bool"):
    """
    从参数字典中获得参数
    """
    v = None

    try:
        v = d[paramkey]
    except Exception as e:
        print(e)
        return None

    if paramtype == "bool":
        if v.lower() == "true":
            return  True
        else:
            return False
    elif paramtype == "float":
        f = 0.0
        try:
            f = float(v)
        except Exception as e:
            print(e)
            f = None
        return f
    elif paramtype == "int":
        f = 0
        try:
            f = int(v)
        except Exception as e:
            print(e)
            f = None
        return f
    else:
        return v

def fiterpathstarnorandspace(filepath):
    """
    过滤文件路径中的"/ " 和" /"
    """
    while True:
        if filepath.find("/ ") != -1:
            filepath = filepath.replace("/ ", "/")
        else:
            break

    while True:
        if filepath.find(" /") != -1:
            filepath = filepath.replace(" /", "/")
        else:
            break

    return filepath



if __name__ == '__main__':
    params = []
    params.append("  sourcepath =E:/data")
    params.append("  sourcepath =  E:/ddd")
    params.append("  sourcepath  = E:/data1   ")
    params.append("log=E:/datalog")
    params.append("   #log=E:/datalog")
    print(parseparams(params))

    if isinstance(params, list):
        print("list")

    if isinstance("mm", str):
        print("str")


    v = ["sgy", "segy"]
    v1 = "segy"
    if v1 in v:
        print("in")
    else:
        print("out")

    print(float("3"))

    tx ="paramfile=E:/ss ss/1.txt"

    print(tx)

    tx = tx.strip("\"")
    print(tx)


    tx = "/   ddd/  dddddd/ddd /  dd/d"
    print(tx)


    print(fiterpathstarnorandspace(tx))

